package com.yjc.blog.interceptor;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
@Slf4j
public class LogInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("------拦截器开始-------");
        log.info("请求地址:{}{}", request.getRequestURI(),request.getMethod());
        log.info("请求的远程地址:{}",request.getRemoteAddr());
        long millis = System.currentTimeMillis();
        request.setAttribute("startTime",millis);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        Long time = (Long) request.getAttribute("startTime");
        log.info("-------interceptor时间为:{}------------------",time - System.currentTimeMillis());

    }
}
